﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compiled_Tests;
using Tedds_Tool.RulesBase;
using LEET.StandardModuals.Instructions;
using LEET.LEETCommon.Model;
using LEET.LEETCommon;
using LEET.Utilities;
using LEET.CommonInterfaces.TestControls;

namespace LEET.StandardModuals.Instructions
{
    public class RuleBase : Instruction, IRuleBaseInstruction
    {
        public RuleBase(String[] args)
            : base(args)
        {
            IEnumerator<String> iter = Rules.GetEnumerator();
            if(iter.MoveNext())
            {
                Name = iter.Current;
                while(iter.MoveNext())
                    Name += " | " + iter.Current;
            }
            InstructionType = NodeTypes.RULE_TYPE;
        }

        public static IAutomationActionResult StaticCall(IExecutableRule [] rules)
        {
            Compiled_Tests.RuleBase.RuleBase rulebase = new Compiled_Tests.RuleBase.RuleBase();
            foreach (IExecutableRule rule in rules)
                rulebase.AddRule(rule);
            return new AutomationActionResult(rulebase);
        }

        public override String RenderStaticCall()
        {
            StringBuilder args = new StringBuilder();
            args.Append("new IExecutableRule [] {");
            IEnumerator<String> iter = Rules.GetEnumerator();
            if(iter.MoveNext())
            {
                args.Append("new ").Append(iter.Current).Append("()");
                while(iter.MoveNext())
                {
                    args.Append(", new ").Append(iter.Current).Append("()");
                }
            }
            args.Append("}");

            return "LEET.StandardModuals.Instructions.RuleBase.StaticCall("+args.ToString()+")";
        }


        public override string PlainText()
        {
            return "Define Rules to be adhered to.";
        }

        public IEnumerable<string> Rules { 
            get 
            {
                for (int i = 1; i < args.Length; i++)
                    yield return Helpers.NormalizeClassName(args[i].Trim());
            } 
        }
    }
}
